package com.miniot.pest.handler;

import com.alibaba.fastjson.JSON;
import com.miniot.business.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther:lsh
 * @Date: 2022-02-02-09:43
 * @Description: com.miniot.base.config
 * @Version: 1.0
 */
@Slf4j
public class ResourceAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("未访问授权", accessDeniedException);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(403);
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(R.fail("未访问授权").setCode(403)));


    }

}
